package suites.page {
	import suites.AbstractConfig;

	import org.flexunit.asserts.assertEquals;
	import org.flexunit.asserts.assertNotNull;
	import org.splink.deepsplink.boot.Bootstrapper;
	import org.splink.deepsplink.boot.IDeepsplinkFacade;
	import org.splink.deepsplink.config.ConfigData;
	import org.splink.deepsplink.config.ConfigDataReader;
	import org.splink.deepsplink.page.request.IRequestBuilder;
	import org.splink.deepsplink.page.request.RequestBuilderFactory;
	import org.splink.library.tree.Tree;

	/**
	 * @author Max Kugland
	 */
	public class RequestBuilderFactoryTest extends AbstractConfig {
		private static const HIDE_INITSHOW_FINALIZE : String = "hide-initshow-finalize";		private static const HIDE_INIT_SHOW_FINALIZE : String = "hide-init-show-finalize";		private static const INITSHOW_HIDE_FINALIZE : String = "initshow-hide-finalize";		private static const INIT_HIDE_SHOW_FINALIZE : String = "init-hide-show-finalize";

		public function createFacory(xml : XML) : RequestBuilderFactory {
			var config : ConfigData = new ConfigDataReader(xml).read();
			var tree : Tree;
			new Bootstrapper(config, createBootStrategy(function(facade : IDeepsplinkFacade):void {
				tree = facade.tree;
			}, 0));
			
			return new RequestBuilderFactory(config, tree, []);
		}

		[Test]
		public function hideInitShowFinalize() : void {
			var factory : RequestBuilderFactory = createFacory(<config>
							<page id={ROOT} clazz={MOCK_PAGE_CP} title={PAGE_TITLE + ROOT} request={HIDE_INITSHOW_FINALIZE}>
								<page id={P_404} clazz={MOCK_PAGE_CP} title={PAGE_TITLE + P_404} />
								<page id={count} clazz={MOCK_PAGE_CP} title={PAGE_TITLE + pageCount} />
								<page id={count} clazz={MOCK_PAGE_CP} title={PAGE_TITLE + pageCount} />
								<page id={count} clazz={MOCK_PAGE_CP} title={PAGE_TITLE + pageCount} />
								<page id={count} clazz={MOCK_PAGE_CP} title={PAGE_TITLE + pageCount} />
							</page>
							<home id={HOME} />
							<notfound redirect={P_404} />
						</config>);
			
			assertEquals(4, pageCount);
			for (var pageId : int = 1;pageId < pageCount;pageId++) {
				var builder : IRequestBuilder = factory.create(pageId.toString());
				assertNotNull(builder);
				assertEquals(HIDE_INITSHOW_FINALIZE, builder.type); 
			}
		}

		[Test]
		public function nestedHideInitShowFinalize() : void {
			var factory : RequestBuilderFactory = createFacory(<config>
							<page id={ROOT} clazz={MOCK_PAGE_CP} title={PAGE_TITLE + ROOT} request={HIDE_INITSHOW_FINALIZE}>
								<page id={P_404} clazz={MOCK_PAGE_CP} title={PAGE_TITLE + P_404} />
								<page id={count} clazz={MOCK_PAGE_CP} title={PAGE_TITLE + pageCount} />
								<page id={count} clazz={MOCK_PAGE_CP} title={PAGE_TITLE + pageCount} >
									<page id={count} clazz={MOCK_PAGE_CP} title={PAGE_TITLE + pageCount} />
									<page id={count} clazz={MOCK_PAGE_CP} title={PAGE_TITLE + pageCount} />
								</page>
								<page id={count} clazz={MOCK_PAGE_CP} title={PAGE_TITLE + pageCount} >
									<page id={count} clazz={MOCK_PAGE_CP} title={PAGE_TITLE + pageCount} >
										<page id={count} clazz={MOCK_PAGE_CP} title={PAGE_TITLE + pageCount} />
									</page>
								</page>
							</page>
							<home id={HOME} />
							<notfound redirect={P_404} />
						</config>);

			assertEquals(7, pageCount);
			for (var pageId : int = 1;pageId < pageCount;pageId++) {
				var builder : IRequestBuilder = factory.create(pageId.toString());
				assertNotNull(builder);
				assertEquals(HIDE_INITSHOW_FINALIZE, builder.type); 
			}
		}

		[Test]
		public function mixed() : void {
			var factory : RequestBuilderFactory = createFacory(<config>
							<page id={ROOT} clazz={MOCK_PAGE_CP} title={PAGE_TITLE + ROOT} request={HIDE_INITSHOW_FINALIZE}>
								<page id={P_404} clazz={MOCK_PAGE_CP} title={PAGE_TITLE + P_404} />
								<page id="1" clazz={MOCK_PAGE_CP} title={PAGE_TITLE + "1"} />
								<page id="2" clazz={MOCK_PAGE_CP} title={PAGE_TITLE + "2"} request={HIDE_INIT_SHOW_FINALIZE} >
									<page id="3" clazz={MOCK_PAGE_CP} title={PAGE_TITLE + "3"} />
									<page id="4" clazz={MOCK_PAGE_CP} title={PAGE_TITLE + "4"} />
								</page>
								<page id="5" clazz={MOCK_PAGE_CP} title={PAGE_TITLE + "5"} request={INITSHOW_HIDE_FINALIZE} >
									<page id="6" clazz={MOCK_PAGE_CP} title={PAGE_TITLE + "6"} >
										<page id="7" clazz={MOCK_PAGE_CP} title={PAGE_TITLE + "7"} />
										<page id="8" clazz={MOCK_PAGE_CP} title={PAGE_TITLE + "8"} request={INIT_HIDE_SHOW_FINALIZE}>
											<page id="9" clazz={MOCK_PAGE_CP} title={PAGE_TITLE + "9"} />
											<page id="10" clazz={MOCK_PAGE_CP} title={PAGE_TITLE + "10"} />
										</page>
									</page>
								</page>
							</page>
							<home id={HOME} />
							<notfound redirect={P_404} />
						</config>);

			for (var pageId : int = 1;pageId < pageCount;pageId++) {
				var builder : IRequestBuilder = factory.create(pageId.toString());
				assertNotNull(builder);
				switch(pageId) {
					case 1:
					case 2:
					case 5:
						assertEquals(HIDE_INITSHOW_FINALIZE, builder.type); 
						break;
					case 3:
					case 4:
						assertEquals(HIDE_INIT_SHOW_FINALIZE, builder.type); 
						break;
					case 6:
					case 7:
					case 8:
						assertEquals(INITSHOW_HIDE_FINALIZE, builder.type); 
						break;
					case 9:
					case 10:
						assertEquals(INIT_HIDE_SHOW_FINALIZE, builder.type); 
						assertEquals(INIT_HIDE_SHOW_FINALIZE, builder.type); 
						break;
				}
			}
		}
	}
}
